﻿var _index = 0;
var _list = null;
var _cur = null;
var _player = null;
var _random = false;
var _repeat = false;
var _retrying = false;
var _timer;
var _lrc = new Lrc();
var _timeLineWidth;
var _timeBarWidth;
function InitPlayer()
{
	_timeLineWidth = $("#TimeLine").width();
	_timeBarWidth = $("#TimeBar").width();
	$("#Player").append("<div id=\"Player16Container\"></div><div id=\"Player21Container\"></div>");
	if (typeof (Player16) != "undefined")
		Player16.WritePlayer(null, 0, 0, null, document.getElementById("Player16Container"));
	if (typeof (Player21) != "undefined")
		Player21.WritePlayer(null, 0, 0, null, document.getElementById("Player21Container"));

	$("#MinMaxTitle").click(function()
	{
		if (this.src.indexOf("min") != -1)
		{
			this.src = this.src.replace("min", "max");
			$("#Title").hide();
			_lrc.Stop();
		}
		else
		{
			this.src = this.src.replace("max", "min");
			$("#Title").show();
			_lrc.Run();
		}
	});

	$("#CloseTitle").click(function()
	{
		$("#Player H5:first").hide();
		$("#Title").hide();
		_lrc.Stop();
	});

	$("#MinMaxPlayList").click(function()
	{
		if (this.src.indexOf("min") != -1)
		{
			this.src = this.src.replace("min", "max");
			$("#PlayList").hide();
		}
		else
		{
			this.src = this.src.replace("max", "min");
			$("#PlayList").show();
		}
	});

	$("#ClosePlayList").click(function()
	{
		$("#Player H5:last").hide();
		$("#PlayList").hide();
	});

	$("#Random").click(function()
	{
		if (_random)
			this.src = this.src.replace("_on.", "_off.");
		else
			this.src = this.src.replace("_off.", "_on.");
		
		_random = !_random;
	});

	$("#Repeat").click(function()
	{
		if (_repeat)
			this.src = this.src.replace("_on.", "_off.");
		else
			this.src = this.src.replace("_off.", "_on.");
		
		_repeat = !_repeat;
	});

	$("#Mute").click(function()
	{
		if (this.src.indexOf("_on.") != -1)
		{
			this.src = this.src.replace("_on.", "_off.");
			_player.SetMute(false);
		}
		else
		{
			this.src = this.src.replace("_off.", "_on.");
			_player.SetMute(true);
		}
	});

	$("#Prev,#Next").mouseover(function()
	{
		this.src = this.src.replace(".gif", "_on.gif");
	});

	$("#Prev,#Next").mouseout(function()
	{
		this.src = this.src.replace("_on.gif", ".gif");
	});

	$("#TimeLine").click(function(e)
	{
		var line = $(this);
		var pos = e.clientX - line.offset().left;
		if ((_lang == "UYG" || _lang == "KZA" || _lang == "KGA") && $.browser.msie)
			pos -= $("#TimeBar").width();
		_player.SetPosition(pos * _player.GetDuration() / line.width());
	});

	$("#TimeBar").draggable(
	{
		axis:"x",
		containment:"#TimeLine",
		stop:function(e, ui)
		{
			var pos = ui.position.left;
			if (_lang == "UYG" || _lang == "KZA" || _lang == "KGA")
				pos = _timeLineWidth - _timeBarWidth + pos;
			_player.SetPosition(pos * _player.GetDuration() / (_timeLineWidth - ui.helper.width()));
		}
	});

	$("#VolumeLine").click(function(e)
	{
		var line = $(this);
		var pos = e.clientX - line.offset().left;
		if (pos < 0) pos = 0;
		if (pos > line.width()) pos = line.width();
		if ((_lang == "UYG" || _lang == "KZA" || _lang == "KGA") && $.browser.msie)
			pos -= $("#VolumeBar").width();
		SetVolume(Math.floor(pos * 100 / line.width()));
	});

	$("#VolumeBar").draggable(
	{
		axis:"x",
		containment:"#VolumeLine",
		stop:function(e, ui)
		{
			var pos = ui.position.left;
			if (_lang == "UYG" || _lang == "KZA" || _lang == "KGA")
				pos = $("#VolumeLine").width() - ui.helper.width() + pos;
			SetVolume(Math.floor(pos * 100 / $("#VolumeLine").width()));
		}
	});

	$("#TrackLine").click(function(e)
	{
		var line = $(this);
		var pos = e.clientX - line.offset().left;
		if (pos < 0) pos = 0;
		if (pos > line.width()) pos = line.width();
		if ((_lang == "UYG" || _lang == "KZA" || _lang == "KGA") && $.browser.msie)
			pos -= $("#TrackBar").width();
		SetBalance(Math.floor((pos * 100 / line.width() - 50) * 2));
	});

	$("#TrackBar").draggable(
	{
		axis:"x",
		containment:"#TrackLine",
		stop:function(e, ui)
		{
			var pos = ui.position.left;
			if (_lang == "UYG" || _lang == "KZA" || _lang == "KGA")
				pos = $("#TrackLine").width() - ui.helper.width() + pos;
			SetBalance(Math.floor((pos * 100 / $("#TrackLine").width() - 50) * 2));
		}
	});

	$("#PlayList a").click(function()
	{
		if (_cur == null) return;
		
		var cur = $(this).parent().eq(0);
		if (cur == _cur)
			return false;
			
		_cur.removeClass("Selected");
		_index = $(this).offsetParent().children().index(cur);
		Play();
		return false;
	});

	$("#PlayList input").click(function()
	{
		_list = $("#PlayList li:has(input:checked)");
	});

	$(".Block15 input:button").click(function()
	{
		$(".Block15 pre").show();
		$(".Block15 form").hide();
	});

	var block10a = $(".Block10 a");
	block10a.click(function()
	{
		var index = block10a.index(this);
		switch (index)
		{
			case 0:
			case 1:
				$(".Block15 pre").hide();
				$(".Block15 form").show();
				$(".Block15 input:hidden").val("Lyric");
				$(".Block15 input:submit").val($(this).text());
				break;
			case 2:
			case 3:
				$(".Block15 pre").hide();
				$(".Block15 form").show();
				$(".Block15 input:hidden").val("Title");
				$(".Block15 input:submit").val($(this).text());
				break;
		}
		return false;
	});

	$(".Block15 form").submit(function()
	{
		var content = $.trim(this.Content.value);
		if (content == "")
		{
			Alert(_lang_content_empty);
			return false;
		}
		
		var guid, name;
		if (_page == "music")
		{
			guid = _guid;
			name = _name;
		}
		else
		{
			guid = _cur.attr("Guid");
			name = _cur.attr("Name");
		}
			
		$.ajax(
		{
			url : _webPath + "alicms.aspx?Object=" + this.Type.value + "&Operation=Add",
			data : "IsChecked=false&Name=" + encodeURIComponent(name) + "&MusicID=" + guid + "&Content=" + encodeURIComponent(content),
			type : "POST",
			error : function (request)
			{
				Alert(_lang_operation_fail);
			},
			success : function (data)
			{
				Alert(_lang_operation_success);
			}
		});
		
		return false;
	});

	$("#PlayList img").click(function()
	{
		var parent = $(this).parent().eq(0);
		var index = parent.children().index(this);
		var id;
		if (_page == "play")
			id = parent.parent().eq(0).attr("InnerID");
		else
			id = _id;
			
		switch (index)
		{
			case 0:
				var download = _page == "play" ? parent.parent().eq(0).attr("Download") : _download;
				window.open(download);
				break;
			case 1:
				$.post(_webPath + "alicms.aspx?Object=Music&Operation=IncrementUp&ID=" + id, {});
				break;
			case 2:
				$.post(_webPath + "alicms.aspx?Object=Music&Operation=IncrementDown&ID=" + id, {});
				break;
			case 3:
				$.post(_webPath + "alicms.aspx?Object=Music&Operation=IncrementError&ID=" + id, {});
				break;
		}
		
		if (index != 0)
		{
			Alert(_lang_operation_success);
			$(this).css("visibility", "hidden");
		}
	});

	$("#ScrollingInfo").marquee((_lang == "UYG" || _lang == "KZA" || _lang == "KGA") ? "right" : "left", 2, 100).mouseover(function(){$(this).trigger("stop");}).mouseout(function(){$(this).trigger("start");});
	$("textarea").attr("wrap", "off");
	$("#Prev").click(Prev);
	$("#Play").click(Play);
	$("#Pause").click(Pause);
	$("#Stop").click(Stop);
	$("#Next").click(Next);
	
	window.onerror = function(){return true};

	_list = $("#PlayList li:has(input:checked)");
	Play();
	SetVolume(50);
	SetBalance(0);
}

function SetStateImage(state)
{
	var button, src;
	
	button = $("#Play");
	src = button.attr("src");
	if (state == "play")
		button.attr("src", src.replace("play.", "play_on."));
	else
		button.attr("src", src.replace("play_on.", "play."));
	
	button = $("#Pause");
	src = button.attr("src");
	if (state == "pause")
		button.attr("src", src.replace("pause.", "pause_on."));
	else
		button.attr("src", src.replace("pause_on.", "pause."));
	
	button = $("#Stop");
	src = button.attr("src");
	if (state == "stop")
		button.attr("src", src.replace("stop.", "stop_on."));
	else
		button.attr("src", src.replace("stop_on.", "stop."));	
}

function Play()
{
	_cur = _list.eq(_index);
	_retrying = false;

	var oldPlayer = _player;
	var playerID;
	if (typeof (_playerID) == "undefined")
		playerID = _cur.attr("PlayerID");
	else
		playerID = _playerID;
	_player = eval("Player" + playerID);
	if (oldPlayer && oldPlayer != _player)
	{
		if (playerID == "16")
			oldPlayer.SetSource("");
		oldPlayer.Stop();
	}
	if (typeof (_url) == "undefined")
		_player.Play(_cur.attr("Url"));
	else
		_player.Play(_url);

	_cur.addClass("Selected");
	SetStateImage("play");
	$("#Time").text("00:00 | 00:00");
	$("#ScrollingInfo").trigger("set", [_cur.text()]);
	$("#Wave").attr("src", $("#Wave").attr("src").replace("_off.", "_on."));
	
	var top = 0;
	if (_cur.position().top < 0)
		top = _cur.position().top + _cur.offsetParent().scrollTop() - _cur.offsetParent().height() / 2;
	else if (_cur.position().top >= _cur.offsetParent().height())
		top = _cur.position().top - _cur.offsetParent().height() + _cur.offsetParent().scrollTop() + _cur.offsetParent().height() / 2;
	if (top != 0)
		_cur.offsetParent().animate({scrollTop:top}, 1000);

	//Title
	var lrc = "";
	if (_page == "music")
		lrc = $("#Title pre").html();
	else
		lrc = _cur.attr("Lrc");
	if ($.trim(lrc) != "")
	{
		_lrc.Init(lrc, document.getElementById(_player.PlayerID), playerID == 16 ? "mp" : "rp", "_lrc", 100);
		_lrc.Run();
	}
	else
	{
		_lrc.Init(null);
	}
	
	//Lyric
	var lyric = "";
	if (_page == "music")
	{
		lyric = $(".Block15 pre").html();
	}
	else
	{
		lyric = _cur.attr("Lyric");
		$(".Block15 pre").text(lyric);
	}
	if ($.trim(lyric) == "")
	{
		$(".Block15 pre").hide();
		$(".Block15 form").show();
	}
	else
	{
		$(".Block15 pre").show();
		$(".Block15 form").hide();
	}
	
	//
	if (_page == "play")
	{
		$(".Block15 h5").html(_cur.attr("Name"));
		$("#HitCount").html(_cur.attr("HitCount"));
		if (_cur.attr("HitCountIncremented") == undefined)
		{
			$.post(_webPath + "alicms.aspx?Object=Music&Operation=IncrementHitCount&ID=" + _cur.attr("InnerID"), {});
			_cur.attr("HitCountIncremented", true);
		}
		$("#Up").html(_cur.attr("Up"));
		$("#Down").html(_cur.attr("Down"));
		if (_cur.attr("RegionUrl") != "")
			$("#Region").html("<a href=\"" + _cur.attr("RegionUrl") + "\">" + _cur.attr("RegionName") + "</a>");
		if (_cur.attr("LanguageUrl") != "")
			$("#Language").html("<a href=\"" + _cur.attr("LanguageUrl") + "\">" + _cur.attr("LanguageName") + "</a>");
		if (_cur.attr("CategoryUrl") != "")
			$("#Category").html("<a href=\"" + _cur.attr("CategoryUrl") + "\">" + _cur.attr("CategoryName") + "</a>");
		if (_cur.attr("TagUrls") != "")
		{
			var html = "";
			var tagUrls = _cur.attr("TagUrls").split("|");
			var tagNames = _cur.attr("TagNames").split("|");
			for (var i = 0; i < tagNames.length; i++)
			{
				if (tagNames[i] == "")
					continue;
				
				html += "<a href=\"" + tagUrls[i] + "\">" + tagNames[i] + "</a>";
			}
			$("#Tag").html(html);
		}
		else
		{
			$("#Tag").html("");
		}
		if (_cur.attr("PersonUrls") != "")
		{
			var html = "";
			var personUrls = _cur.attr("PersonUrls").split("|");
			var personNames = _cur.attr("PersonNames").split("|");
			for (var i = 0; i < personNames.length; i++)
			{
				if (personNames[i] == "")
					continue;
				
				html += "<a href=\"" + personUrls[i] + "\">" + personNames[i] + "</a>";
			}
			$("#Person").html(html);
		}
		else
		{
			$("#Person").html("");
		}
		if (_cur.attr("AlbumUrls") != "")
		{
			var html = "";
			var albumUrls = _cur.attr("AlbumUrls").split("|");
			var albumNames = _cur.attr("AlbumNames").split("|");
			for (var i = 0; i < albumNames.length; i++)
			{
				if (albumNames[i] == "")
					continue;
				
				html += "<a href=\"" + albumUrls[i] + "\">" + albumNames[i] + "</a>";
			}
			$("#Album").html(html);
		}
		else
		{
			$("#Album").html("");
		}
	}
	
	_timer = setTimeout("OnTimer()", 1000);
}

function Pause()
{
	if (_cur == null) return;
	
	clearTimeout(_timer);
	$("#Wave").attr("src", $("#Wave").attr("src").replace("_on.", "_off."));
	_player.Pause();
	SetStateImage("pause");
	window.status = "Paused";
}

function Stop()
{
	if (_cur == null) return;
	
	clearTimeout(_timer);
	$("#Wave").attr("src", $("#Wave").attr("src").replace("_on.", "_off."));
	_player.Stop();
	SetStateImage("stop");
	window.status = "Stopped";
}

function Prev()
{
	if (_cur == null) return;
	
	clearTimeout(_timer);
	_cur.removeClass("Selected");
	
	_index--;
	if (_index < 0)
		_index = _list.length - 1;
	Play();
}

function Next()
{
	if (_cur == null) return;
	
	clearTimeout(_timer);
	_cur.removeClass("Selected");
	
	_index++;
	if (_index >= _list.length)
		_index = 0;
	Play();
}

function SetVolume(volume)
{
	_player.SetVolume(volume);
	
	var lineWidth = $("#VolumeLine").width();
	var bar = $("#VolumeBar");
	var left = volume * lineWidth / 100;
	if (_lang == "UYG" || _lang == "KZA" || _lang == "KGA")
		left = -(lineWidth - bar.width() - left);
	bar.css("left", left);
}

function SetBalance(balance)
{
	_player.SetBalance(balance);
	
	var lineWidth = $("#TrackLine").width();
	var bar = $("#TrackBar");
	var left = balance * lineWidth / 200 + 50 * lineWidth / 100;
	if (_lang == "UYG" || _lang == "KZA" || _lang == "KGA")
		left = -(lineWidth - bar.width() - left);
	bar.css("left", left);
}

function OnTimer()
{
	var state = _player.GetPlayState();
	window.status = state;
	if (state == "Playing")
	{
		var position = _player.GetPosition();
		var duration = _player.GetDuration();
		if (duration >= position)
		{
			var minute, second, time;
			minute = parseInt(Math.round(position) / 60);
			second = Math.round(position) % 60;
			time = (minute.toString().length == 1 ? "0" + minute : minute.toString()) + ":" + (second.toString().length == 1 ? "0" + second : second.toString());
			minute = parseInt(Math.round(duration) / 60);
			second = Math.round(duration) % 60;
			time += " | " + (minute.toString().length == 1 ? "0" + minute : minute.toString()) + ":" + (second.toString().length == 1 ? "0" + second : second.toString());
			$("#Time").text(time);
			
			var left = Math.round(position * (_timeLineWidth - _timeBarWidth) / duration);
			if (_lang == "UYG" || _lang == "KZA" || _lang == "KGA")
				left = -(_timeLineWidth - _timeBarWidth - left);
			$("#TimeBar").css("left", left);
		}
	}
	else if (state == "Stopped" || state == "Ready")
	{
		var playerID;
		if (typeof (_playerID) == "undefined")
			playerID = _cur.attr("PlayerID");
		else
			playerID = _playerID;
			
		if (playerID == "21" && !_retrying)
		{
			_retrying = true;
			window.status = "retrying...";
			_timer = setTimeout("OnTimer()", 10000);
			return;
		}
		
		if (_repeat)
		{
			Play();
		}
		else if (_random)
		{
			if (_list.length == 0)
				return;
				
			_cur.removeClass("Selected");
			_index = Math.random() * _list.length;
			Play();
		}
		else
		{
			if (_list.length == 0)
				return;
				
			Next();
		}

		return;
	}
	
	_timer = setTimeout("OnTimer()", 1000);
}